package com.cupk.inspection.service;

import com.cupk.inspection.model.Task;
import com.cupk.inspection.repository.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class TaskService {

    @Autowired
    private TaskRepository taskRepository;

    // 创建任务
    public Task createTask(Task task) {
        task.setCreated_at(new java.sql.Timestamp(System.currentTimeMillis()));
        return taskRepository.save(task);
    }

    // 查询任务
    public Optional<Task> getTaskById(Long id) {
        return taskRepository.findById(id);
    }

    // 更新任务状态
    public Task updateTaskStatus(Long id, String status) {
        Optional<Task> taskOptional = taskRepository.findById(id);
        if (taskOptional.isPresent()) {
            Task task = taskOptional.get();
            task.setStatus(status);
            return taskRepository.save(task);
        }
        return null;
    }

    // 获取所有任务
    public Iterable<Task> getAllTasks() {
        return taskRepository.findAll();
    }
}